package com.daaug.demo.robotlegs.texttospeech.views
{
	import com.daaug.demo.robotlegs.texttospeech.events.TTSEvent;
	import com.daaug.demo.robotlegs.texttospeech.models.TxtAudioModel;
	import com.daaug.demo.robotlegs.texttospeech.models.TxtAudioModelEvent;
	
	import org.robotlegs.mvcs.Mediator;
	
	public class TTSFormMediator extends Mediator
	{
		[Inject]
		public var view:TTSForm;
		
		[Inject]
		public var txtAudio:TxtAudioModel;
		
		override public function onRegister():void {
			view.addEventListener(TTSFormEvent.SUBMIT, onFormSubmit);
			
			eventDispatcher.addEventListener(TxtAudioModelEvent.UPDATE, onTxtAudioUpdate);
		}
		
		private function onFormSubmit(event:TTSFormEvent):void {
			var ttse:TTSEvent = new TTSEvent(TTSEvent.CONVERT_TEXT, view.userText.text, true, false);
			eventDispatcher.dispatchEvent(ttse);
			view.audioControl.enabled = false;
		}
		
		private function onTxtAudioUpdate(event:TxtAudioModelEvent):void {
			view.audioControl.sound = txtAudio.audio;
			view.audioControl.enabled = true;
		}
	}
}